package com.qfedu.luck.dao.impl;

import com.qfedu.luck.dao.UserDao;
import com.qfedu.luck.entity.User;
import com.qfedu.luck.util.JdbcUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: LuckStudy
 * @description:
 * @author: Feri(邢朋辉)
 * @create: 2021-02-24 14:22
 */
public class UserDaoImpl implements UserDao {
    @Override
    public int insert(User user) {
        String sql="insert into t_user(phone,pwd,flag,ctime) values(?,?,1,now())";
        Map<Integer,Object> map=new HashMap<>();
        map.put(1,user.getPhone());
        map.put(2,user.getPwd());
        return JdbcUtil.execu(sql,map);
    }

    @Override
    public User selectByPhone(String phone) {
        ResultSet rs=JdbcUtil.execuSingle("select * from t_user where phone="+phone);
        if(rs!=null){
            try {
                if(rs.next()){
                    User user=new User();
                    user.setId(rs.getInt("id"));
                    user.setPhone(rs.getString("phone"));
                    user.setPwd(rs.getString("pwd"));
                    user.setFlag(rs.getInt("flag"));
                    user.setCtime(rs.getDate("ctime"));
                    rs.close();
                    return user;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return null;
    }
}
